package com.eqdd.yiqidian.ui.gongneng.renshi.zuzhiguanli;

import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.eqdd.yiqidian.AddCareerActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.EditBean;
import com.eqdd.yiqidian.bean.rebuild.DepartmentListResultBean;
import com.eqdd.yiqidian.bean.utils.CareerChooseBean;
import com.eqdd.yiqidian.bean.utils.MultiChooseBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.ui.base.BaseEditActivity;
import com.eqdd.yiqidian.utils.MultiChooseTestUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

import chihane.jdaddressselector.BottomDialog;

public class AddCareerActivity extends BaseEditActivity {
    private AddCareerActivityCustom dataBinding;


    private String[] titles = new String[]{
            "所属部门",
            "职位名称",
            "职位类型"
    };
    private String[] titleTexts = new String[]{
            "所属部门",
            "职位名称",
            "职位类型"
    };
    private String[] hintTexts = new String[]{
            "",
            "请输入",
            "请选择"
    };
    private String[] tipTexts = new String[]{
            "职位名称",
            "职位名称",
            "职位类型"
    };
    private int[] types = new int[]{
            4, 0, 4
    };
    private String msg;
    private List<CareerChooseBean> carrerBeans;
    private DepartmentListResultBean departmentListResultBean;
    private BottomDialog careerDialog;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_add_career);
        initTopTitleBar(View.VISIBLE, "添加职位");
        initTopRightText("添加", (v) -> {
            if (checkInfo()) {
                OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.ADD_CAREER)
                        .params("companyId", departmentListResultBean.getCompanyId())
                        .params("departId", departmentListResultBean.getDepartId())
                        .params("name", editBeans.get(1).getContent())
                        .params("type", editBeans.get(2).getContent())
                        .params("desc", "123")
                        .params("userGuid", user.getGuid())
                        .params("isleader", dataBinding.includeSb.sbEnd.isChecked() ? "true" : "false")
                        .execute(new DialogCallBack<HttpResult>(this) {
                            @Override
                            public void onSuccess(Response<HttpResult> response) {
                                HttpResult httpResult = response.body();
                                ToastUtil.showShort(httpResult.getMsg());
                                if (httpResult.getStatus() == 200) {
                                    setResult(Config.SUCCESS);
                                    finish();
                                }
                            }
                        });
            }
        });
    }

    private boolean checkInfo() {
        if (TextUtils.isEmpty(((TextView) quickAdapter.getViewByPosition(1, R.id.tv_vertical_content)).getText().toString().trim())) {
            msg = "请输入职位名称";
            return false;
        }
        return true;
    }

    @Override
    public void initData() {
        departmentListResultBean = getIntent().getParcelableExtra(Config.DEPARTMENT);
        for (int i = 0; i < titles.length; i++) {
            EditBean editBean = new EditBean();
            editBean.setTitle(titles[i]);
            editBean.setType(types[i]);
            editBean.setContent("");
            editBean.setTextTip(tipTexts[i]);
            editBean.setTextTitle(titleTexts[i]);
            editBean.setHint(hintTexts[i]);
            editBean.setEnter(false);
            editBeans.add(editBean);
            if (i == 0) {
                editBean.setContent(departmentListResultBean.getDepartName());
            }
        }
        OkGo.<List<CareerChooseBean>>get(HttpConfig.BASE_URL + HttpConfig.AREA_OPTIONS)
                .params("type", 1)
                .cacheKey(HttpConfig.BASE_URL + HttpConfig.AREA_OPTIONS + "1")
                .cacheMode(CacheMode.IF_NONE_CACHE_REQUEST)
                .execute(new DialogCallBack<List<CareerChooseBean>>(this) {
                    @Override
                    public void onSuccess(Response<List<CareerChooseBean>> response) {
                        carrerBeans = response.body();
                        initCareerPicker();
                    }

                    @Override
                    public void onCacheSuccess(Response<List<CareerChooseBean>> response) {
                        super.onCacheSuccess(response);
                        carrerBeans = response.body();
                        initCareerPicker();
                    }
                });
        dataBinding.includeSb.tvLeftCenterContent.setText("是否具有审批功能");
    }

    private void initCareerPicker() {
        ArrayList<MultiChooseBean> multiChooseBeen = new ArrayList<>();
        multiChooseBeen.addAll(carrerBeans);
        careerDialog = new MultiChooseTestUtil().getAreaDialog(this, 2, multiChooseBeen, (selectIds, selectAbles) ->{
                careerDialog.hide();
                quickAdapter.getData().get(2).setContent(selectAbles.get(1).getName());
                quickAdapter.notifyItemChanged(2);
        });
    }


    @Override
    protected RecyclerView initRecyclerView() {
        return dataBinding.recyclerView;
    }

    @Override
    public void itemClick(int p) {

        if (p == 2) {
            careerDialog.show();
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }
}
